package com.hp.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {

	UserDao dao=new UserDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String idStr=request.getParameter("id");
		int id=0;
		if (idStr !=null) {
			id=new Integer(idStr);
					
		}
		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		String sex = request.getParameter("sex");
		
		String[] hobbys=request.getParameterValues("hobby");
		String hobbyStr ="";
		if (hobbys !=null) {
			for(String hobby: hobbys) {
				hobbyStr += hobby+",";
			}
			hobbyStr=hobbyStr.substring(0, hobbyStr.length()-1);
		}
		User ue=new User(id, name, pwd);
		dao.updateUser(ue);
		response.sendRedirect("QueryServlet");
	}

}
